package com.ruoyi.ora.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.ora.domain.MemberLevelDesc;
import com.ruoyi.ora.mapper.MemberLevelMapper;
import com.ruoyi.ora.domain.MemberLevel;
import com.ruoyi.ora.service.IMemberLevelService;

/**
 * 会员等级配置Service业务层处理
 * 
 * @author liuwp
 * @date 2024-11-25
 */
@Service
public class MemberLevelServiceImpl implements IMemberLevelService 
{
    @Autowired
    private MemberLevelMapper memberLevelMapper;

    /**
     * 查询会员等级配置
     * 
     * @param id 会员等级配置主键
     * @return 会员等级配置
     */
    @Override
    public MemberLevel selectMemberLevelById(Long id)
    {
        return memberLevelMapper.selectMemberLevelById(id);
    }

    /**
     * 查询会员等级配置列表
     * 
     * @param memberLevel 会员等级配置
     * @return 会员等级配置
     */
    @Override
    public List<MemberLevel> selectMemberLevelList(MemberLevel memberLevel)
    {
        return memberLevelMapper.selectMemberLevelList(memberLevel);
    }

    /**
     * 根据业务主键查询一条记录：会员等级配置
     *
     * @param memberLevel 会员等级配置
     * @return 会员等级配置
     */
    @Override
    public MemberLevel selectOneMemberLevel(MemberLevel memberLevel)
    {
        return memberLevelMapper.selectOneMemberLevel(memberLevel);
    }

    /**
     * 新增会员等级配置
     * 
     * @param memberLevel 会员等级配置
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMemberLevel(MemberLevel memberLevel)
    {
        int rows = memberLevelMapper.insertMemberLevel(memberLevel);
        insertMemberLevelDesc(memberLevel);
        return rows;
    }

    /**
     * 修改会员等级配置
     * 
     * @param memberLevel 会员等级配置
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMemberLevel(MemberLevel memberLevel)
    {
        memberLevelMapper.deleteMemberLevelDescByMemberLevelId(memberLevel.getId());
        insertMemberLevelDesc(memberLevel);
        return memberLevelMapper.updateMemberLevel(memberLevel);
    }

    @Override
    public int updateMemberLevelCondition(MemberLevel memberLevel, MemberLevel memberLevelCondition)
    {
                memberLevelMapper.deleteMemberLevelDescByMemberLevelId(memberLevel.getId());
            insertMemberLevelDesc(memberLevel);
        return memberLevelMapper.updateMemberLevelCondition(memberLevel, memberLevelCondition);
    }

    /**
     * 批量删除会员等级配置
     * 
     * @param ids 需要删除的会员等级配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMemberLevelByIds(Long[] ids)
    {
        memberLevelMapper.deleteMemberLevelDescByMemberLevelIds(ids);
        return memberLevelMapper.deleteMemberLevelByIds(ids);
    }

    /**
     * 删除会员等级配置信息
     * 
     * @param id 会员等级配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMemberLevelById(Long id)
    {
        memberLevelMapper.deleteMemberLevelDescByMemberLevelId(id);
        return memberLevelMapper.deleteMemberLevelById(id);
    }

    /**
     * 条件删除会员等级配置
     *
     * @param memberLevel 会员等级配置
     * @return 结果
     */
    @Override
    public int deleteMemberLevelByCondition(MemberLevel memberLevel)
    {
        return memberLevelMapper.deleteMemberLevelByCondition(memberLevel);
    }


    /**
     * 新增会员等级配置多语言信息
     * 
     * @param memberLevel 会员等级配置对象
     */
    public void insertMemberLevelDesc(MemberLevel memberLevel)
    {
        List<MemberLevelDesc> memberLevelDescList = memberLevel.getMemberLevelDescList();
        Long id = memberLevel.getId();
        if (StringUtils.isNotNull(memberLevelDescList))
        {
            List<MemberLevelDesc> list = new ArrayList<MemberLevelDesc>();
            for (MemberLevelDesc memberLevelDesc : memberLevelDescList)
            {
                memberLevelDesc.setMemberLevelId(id);
                list.add(memberLevelDesc);
            }
            if (list.size() > 0)
            {
                memberLevelMapper.batchMemberLevelDesc(list);
            }
        }
    }
}
